using System.Collections;
using System.Collections.Generic;
using GG.Tool;
using UnityEngine;
using GGameFramework.Localization;

namespace GGameFramework.Runtime
{
    public class BaseComponent : FrameworkComponent
    {
        private const int DefaultDpi = 96; // default windows dpi

        private float m_GameSpeedBeforePause = 1f;

        public override FrameworkComponentPriority Priority => FrameworkComponentPriority.Core;

        [SerializeField, ChineseName("编辑语言")] private Language m_EditorLanguage = Language.Unspecified;

        [SerializeField, ChineseName("运行帧率")] private int m_FrameRate = 119;

        [SerializeField, ChineseName("游戏速度")] private float m_GameSpeed = 1f;

        [SerializeField, ChineseName("后台运行")] private bool m_RunInBackground = true;

        [SerializeField, ChineseName("是否休眠")] private bool m_NeverSleep = true;

        /// <summary>
        /// 获取或设置编辑器语言（仅编辑器内有效）。
        /// </summary>
        public Language EditorLanguage
        {
            get { return m_EditorLanguage; }
            set { m_EditorLanguage = value; }
        }

        /// <summary>
        /// 获取或设置游戏帧率。
        /// </summary>
        public int FrameRate
        {
            get { return m_FrameRate; }
            set { Application.targetFrameRate = m_FrameRate = value; }
        }

        /// <summary>
        /// 获取或设置游戏速度。
        /// </summary>
        public float GameSpeed
        {
            get { return m_GameSpeed; }
            set { Time.timeScale = m_GameSpeed = value >= 0f ? value : 0f; }
        }

        /// <summary>
        /// 获取游戏是否暂停。
        /// </summary>
        public bool IsGamePaused
        {
            get { return m_GameSpeed <= 0f; }
        }

        /// <summary>
        /// 获取是否正常游戏速度。
        /// </summary>
        public bool IsNormalGameSpeed
        {
            get { return m_GameSpeed == 1f; }
        }

        /// <summary>
        /// 获取或设置是否允许后台运行。
        /// </summary>
        public bool RunInBackground
        {
            get { return m_RunInBackground; }
            set { Application.runInBackground = m_RunInBackground = value; }
        }

        /// <summary>
        /// 获取或设置是否禁止休眠。
        /// </summary>
        public bool NeverSleep
        {
            get { return m_NeverSleep; }
            set
            {
                m_NeverSleep = value;
                Screen.sleepTimeout = value ? SleepTimeout.NeverSleep : SleepTimeout.SystemSetting;
            }
        }

        private void Update()
        {
            FrameworkEntry.Update(Time.deltaTime, Time.unscaledDeltaTime);
        }

        private void OnApplicationQuit()
        {
#if UNITY_5_6_OR_NEWER
            Application.lowMemory -= OnLowMemory;
#endif
            StopAllCoroutines();
        }

        private void OnLowMemory()
        {
            Debug.Log("Low memory reported...");

            var poolComponent = ComponentEntry.GetSystem<PoolComponent>();
            // if (poolComponent != null)
            // {
            //     poolComponent.ClearAllPools();
            // }
            //TODO 资源加载充值
        }
    }
}